
package rentalgui;

import database.LeaseDao;
import database.PaymentDao;
import database.PropertyDao;
import database.TenantDao;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import rentaldb.Lease;
import rentaldb.Property;
import rentaldb.Tenant;

public class LeaseUpdateDeleteWindow extends javax.swing.JFrame {

    /**
     * Creates new form ExpenseUpdateDeleteWindow
     */
    public LeaseUpdateDeleteWindow(LeaseDao ldao, PaymentDao paydao, TenantDao tdao, PropertyDao pdao) {
        initComponents();
        updateMode = false;
        this.ldao = ldao;
        this.paydao = paydao;
        this.tdao = tdao;
        this.pdao = pdao;        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        searchButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        updateButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        inputBox = new javax.swing.JTextField();
        clearButton = new javax.swing.JButton();
        inputPropertyId = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        inputTenantId = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        inputStartDate = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        inputEndDate = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        inputLeaseId = new javax.swing.JTextField();
        inputDeposit = new javax.swing.JTextField();
        inputMonthlyPayment = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Lease Update/Delete");

        jLabel7.setText("Lease ID");

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.setEnabled(false);
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        updateButton.setText("Update");
        updateButton.setEnabled(false);
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        clearButton.setText("Clear");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        inputPropertyId.setEditable(false);

        jLabel4.setText("Monthly Payment");

        inputTenantId.setEditable(false);

        jLabel8.setText("Tenant ID");

        jLabel5.setText("Deposit");

        jLabel3.setText("End Date");

        inputStartDate.setEditable(false);

        jLabel2.setText("Start Date");

        inputEndDate.setEditable(false);

        jLabel9.setText("Property ID");

        jLabel1.setText("Lease ID");

        inputLeaseId.setEditable(false);

        inputDeposit.setEditable(false);

        inputMonthlyPayment.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE))
                            .addComponent(inputBox))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(exitButton, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(inputPropertyId, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
                            .addComponent(inputTenantId, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputLeaseId)
                            .addComponent(inputStartDate, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(inputEndDate)
                            .addComponent(inputMonthlyPayment)
                            .addComponent(inputDeposit))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(inputBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchButton)
                    .addComponent(clearButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(inputLeaseId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(inputEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(inputMonthlyPayment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(inputDeposit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputTenantId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputPropertyId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exitButton)
                    .addComponent(updateButton)
                    .addComponent(deleteButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        clearTextBoxes();
        setEditableStatus(false);
        updateMode = false;
        updateButton.setEnabled(false);
        deleteButton.setEnabled(false);
    }//GEN-LAST:event_clearButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try{
            Integer input = Integer.parseInt(inputBox.getText().trim());
            curLease = ldao.getLeaseById(input);
            putCurrentInTextBoxes();
            setEditableStatus(true);
            updateMode = false;
            updateButton.setEnabled(true);
            deleteButton.setEnabled(true);
        }
        catch(Exception e){
            System.out.println(e);
            JOptionPane.showMessageDialog(null, "Invalid ID.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }        
    }//GEN-LAST:event_searchButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int input = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this entry?", "Delete?", JOptionPane.YES_NO_OPTION);
        if(input == JOptionPane.YES_OPTION){
            try{
                ldao.delete(curLease);
                clearButtonActionPerformed(evt);
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(null, "Error during delete.", "Delete Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateButtonActionPerformed
        if(!updateMode){
            updateMode = true;
            JOptionPane.showMessageDialog(null, "Make the desired changes to the fields, then click update again.", "Update Mode", JOptionPane.INFORMATION_MESSAGE);
            deleteButton.setEnabled(false);
            setEditableStatus(true);
        }else{
            updateMode = false;
            deleteButton.setEnabled(true);
            int input = JOptionPane.showConfirmDialog(null, "Are you sure you want to update this entry?", "Update?", JOptionPane.YES_NO_OPTION);
            if(input == JOptionPane.YES_OPTION){
                try{
                    Integer leaseId = Integer.parseInt(inputLeaseId.getText());
                    Date startDate = null;
                    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
                    startDate = df.parse(inputStartDate.getText());
                    Date endDate = endDate = df.parse(inputEndDate.getText());
                    Double deposit = Double.parseDouble(inputDeposit.getText());
                    Integer tenantId = Integer.parseInt(inputTenantId.getText());
                    Tenant tenant = tdao.getTenantById(tenantId);
                    Integer propertyId = Integer.parseInt(inputPropertyId.getText());
                    Property property = pdao.getPropertyById(propertyId);

                    Lease lease = new Lease();
                    lease.setLeaseid(leaseId);
                    lease.setStartdate(startDate);
                    lease.setEnddate(endDate);
                    lease.setDeposit(deposit);           
                    lease.setTenantid(tenant);                
                    lease.setPropertyid(property);
                    ldao.update(lease);
                }
                catch(Exception e){
                    JOptionPane.showMessageDialog(null, "Error during update.", "Update Error", JOptionPane.ERROR_MESSAGE);
                }
            } else{
                JOptionPane.showMessageDialog(this, "Changes Reset", "Update Cancelled", JOptionPane.INFORMATION_MESSAGE);
                putCurrentInTextBoxes();
            }
            deleteButton.setEnabled(false);
            setEditableStatus(false);
        }
    }//GEN-LAST:event_updateButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        dispose();
    }//GEN-LAST:event_exitButtonActionPerformed

    private void setEditableStatus(boolean setting){
        inputBox.setEditable(setting);
        inputStartDate.setEditable(setting);
        inputEndDate.setEditable(setting);
        inputMonthlyPayment.setEditable(setting);
        inputDeposit.setEditable(setting);
        inputTenantId.setEditable(setting);
        inputPropertyId.setEditable(setting);
    }
    
    private void clearTextBoxes(){
        inputBox.setText("");
        inputLeaseId.setText("");
        inputStartDate.setText("");
        inputEndDate.setText("");
        inputMonthlyPayment.setText("");
        inputDeposit.setText("");
        inputTenantId.setText("");
        inputPropertyId.setText("");
    }
    
    private void putCurrentInTextBoxes(){
        inputLeaseId.setText(curLease.getLeaseid().toString().trim());
        inputStartDate.setText(curLease.getStartdate().toString().trim());
        inputEndDate.setText(curLease.getEnddate().toString().trim());
        inputMonthlyPayment.setText(curLease.getMonthlypayment().toString().trim());
        inputDeposit.setText(curLease.getDeposit().toString().trim());
//        Collection<Payment> paymentIds = curLease.getPaymentCollection();
//        String paymentString = "";
//        for(Payment p : paymentIds){
//            paymentString += p.getPaymentId() +", ";
//        }
//        inputPaymentIds.setText(paymentString.trim());
        inputTenantId.setText(curLease.getTenantid().toString().trim());
        inputPropertyId.setText(curLease.getPropertyid().toString().trim());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton clearButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JTextField inputBox;
    private javax.swing.JTextField inputDeposit;
    private javax.swing.JTextField inputEndDate;
    private javax.swing.JTextField inputLeaseId;
    private javax.swing.JTextField inputMonthlyPayment;
    private javax.swing.JTextField inputPropertyId;
    private javax.swing.JTextField inputStartDate;
    private javax.swing.JTextField inputTenantId;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton searchButton;
    private javax.swing.JButton updateButton;
    // End of variables declaration//GEN-END:variables
    private Lease curLease;
    LeaseDao ldao;
    PaymentDao paydao;
    TenantDao tdao;
    PropertyDao pdao;
    private boolean updateMode;
}
